package com.hwh.springdatajpademo.controller;

import com.hwh.springdatajpademo.pojo.Role;
import com.hwh.springdatajpademo.pojo.User;
import com.hwh.springdatajpademo.repository.RoleRepository;
import com.hwh.springdatajpademo.repository.UserRepository;
import com.hwh.springdatajpademo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Slice;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.transaction.Transactional;
import java.util.List;
import java.util.Random;
import java.util.Set;

@RestController
public class TestController {
    @Autowired
    UserRepository repository;
    @Autowired
    RoleRepository roleRepository;
    @Autowired
    UserService userService;


    @GetMapping("/test1")
    @Transactional
    public void testUser(){

        //单表查询全部
        System.out.println("=============单表查询全部===========");
        System.out.println(repository.findAll());
        System.out.println("==================================");
        System.out.println("=============单表模糊查询===========");
        //单表模糊查询
        System.out.println(repository.findAllByNameLike("z"));
        System.out.println("==================================");
        System.out.println("=============单表多条件查询===========");
        //单表多条件查询
        System.out.println(repository.gainUserListByUsernameOrEmail("hwh","123"));
        System.out.println("==================================");
        System.out.println("=============自定义sql查询===========");
        //自定义sql查询
        System.out.println(repository.findUsersByRoleName("员工"));
        System.out.println("==================================");
        System.out.println("=============更新和删除测试 @Modify注解===========");
        //repository.deleteUser(repository.findUsersByRoleName("员工").get(0).getId());
        //repository.updateUser(repository.findUsersByRoleName("管理员").get(0).getId(),"更新","778978@qq.com");
        //多表查询 查询某角色下的所有用户
        System.out.println("==================================");
        System.out.println("=============多表查询 一对多 查询某角色下的所有用户===========");
        Role role = roleRepository.findByRoleName("管理员");
        Set<User> users = role.getUsers();
        users.forEach(System.out::println);
        System.out.println("==================================");
        System.out.println("=============按name排序找到前10===========");
        List<User> topUsers = repository.findTop10By(Sort.by(Sort.Direction.DESC,"name"));
        topUsers.forEach(System.out::println);
        System.out.println("==================================");
        System.out.println("=============dsl根据用户名和邮箱查询一个用户===========");
        System.out.println(userService.findByNameAndEmail("更新","778978@qq.com"));
        System.out.println("==================================");
        System.out.println("=============dsl根据用户名升序查找所有用户===========");
        userService.findAllByNameOrderDesc().forEach(System.out::println);
        System.out.println("==================================");
        System.out.println("=============dsl部分字段映射查询===========");
        userService.findAllUserDTO(PageRequest.of(0,5));
        System.out.println("==================================");
        System.out.println("=============dsl动态条件查询===========");
        User predicateUser = new User();
        predicateUser.setName("更新");
        userService.findUserByPredicate(predicateUser).forEach(System.out::println);
        System.out.println("==================================");
        userService.findAllByUserRoleDTO().forEach(System.out::println);
    }
    @GetMapping("/test2")
    public void test2(){

        //单表插入+根据角色名精确查询
        for (int i = 0; i < 10; i++) {
            User user = new User();
            String name = String.valueOf(new Random().nextInt(999)+100);
            user.setName(name);
            user.setEmail(name+"@qq.com");
            String[] roleName = {"管理员","员工"};
            String role = roleName[new Random().nextInt(2)];
            user.setRoleId(roleRepository.findByRoleName(role).getId());
            repository.save(user);
        }
    }


}
